//
//  PictureSelectorController.m
//  图片选择器
//
//  Created by zxy on 2019/1/18.
//  Copyright © 2019 zxy. All rights reserved.
//

#import "PictureSelectorController.h"
#import "PictureSelectorCell.h"
#import "PictureSelectorFlowLayout.h"
#import "UIImage+ImageWithScale.h"
@interface PictureSelectorController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,PictureSelectorCellDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,strong)UICollectionView *pictuteSelectorCollectionView;
@property(nonatomic,strong)UIImagePickerController *pickerController;
@property(nonatomic,strong)NSMutableArray *selectetImageArray;
@end

static NSString *const ZXYPictureSelctorCellReuseIdentifier = @"ZXYPictureSelctorCellReuseIdentifier";

@implementation PictureSelectorController

-(NSMutableArray *)selectetImageArray{
    if(!_selectetImageArray){
        _selectetImageArray = [NSMutableArray array];
    }
    return _selectetImageArray;
}

-(UIImagePickerController *)pickerController{
    if(!_pickerController){
        _pickerController = [[UIImagePickerController alloc]init];
//        _pickerController.allowsEditing = YES;
        _pickerController.delegate = self;
    }
    return _pickerController;
}

-(UICollectionView *)pictuteSelectorCollectionView{
    if(!_pictuteSelectorCollectionView){
        PictureSelectorFlowLayout *layout = [[PictureSelectorFlowLayout alloc]init];
        _pictuteSelectorCollectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
        _pictuteSelectorCollectionView.delegate = self;
        _pictuteSelectorCollectionView.dataSource = self;
        [_pictuteSelectorCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([PictureSelectorCell class]) bundle:nil] forCellWithReuseIdentifier:ZXYPictureSelctorCellReuseIdentifier];
    }
    return _pictuteSelectorCollectionView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.pictuteSelectorCollectionView];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.selectetImageArray.count + 1;//+1:至少会显示添加按钮
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PictureSelectorCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ZXYPictureSelctorCellReuseIdentifier forIndexPath:indexPath];
    //相等设置为空，显示添加按钮
    cell.selectedImage = (self.selectetImageArray.count == indexPath.item) ? nil : self.selectetImageArray[indexPath.item];
    cell.delegate = self;
    return cell;
}

#pragma mark -PictureSelectorCellDelegate
-(void)addButtonClick:(PictureSelectorCell *)cell{
    //判断源类型是否可用
    if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
        NSLog(@"类型不可用");
        return;
    }
    [self presentViewController:self.pickerController animated:YES completion:nil];
}

//cell:当前cell
-(void)removeButtonClick:(PictureSelectorCell *)cell{
    NSIndexPath *indexPath = [self.pictuteSelectorCollectionView indexPathForCell:cell];
    [self.selectetImageArray removeObjectAtIndex:indexPath.item];
    [self.pictuteSelectorCollectionView reloadData];
}


#pragma mark -UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info{
    NSLog(@"%@",info);
    
    NSLog(@"--------------------------");
    
    //获取选择图片
    UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
    
    //使用绘图绘制出指定宽度的图片，优化内存，并且图片不会失真
    UIImage *scaleImage = [originalImage imageScaleWidth:200];
    
    [self.selectetImageArray addObject:scaleImage];
    
    [self.pictuteSelectorCollectionView reloadData];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
